package com.sztest.jayjun.mmkv;

import com.sztest.jayjun.MyApplication;
import com.sztest.jayjun.bean.DebugBean;
import com.sztest.jayjun.bean.DebugItemBean;
import com.sztest.jayjun.bean.MainBean;
import com.tencent.mmkv.MMKV;


public class MMKVUtils implements MMKVI {
    private static volatile MMKVUtils ourInstance;
    MMKV mKV = null;
    private MMKVUtils() {
    }
    public void init(){
        MMKV.initialize(MyApplication.getInstance(),MyApplication.getInstance().getExternalFilesDir("mmkv").getAbsolutePath());
        mKV= MMKV.defaultMMKV();
    }
    public static MMKVUtils getInstance() {
        if (null == ourInstance) {
            synchronized(MMKVUtils.class) {
                if (null == ourInstance) {
                    ourInstance = new MMKVUtils();
                }
            }
        }
        return ourInstance;
    }

    @Override
    public void setBoolean(String key, boolean value) {
        mKV.encode(key, value);
    }

    @Override
    public boolean getBoolean(String key) {
        return  mKV.decodeBool(key);
    }

    @Override
    public boolean getBoolean(String key, boolean def) {
        return  mKV.decodeBool(key, def);
    }

    @Override
    public void setInt(String key, int value) {
        mKV.encode(key, value);
    }

    @Override
    public int getInt(String key) {
        return mKV.decodeInt(key);
    }

    @Override
    public int getInt(String key, int def) {
        return mKV.decodeInt(key, def);
    }

    @Override
    public void setString(String key, String str) {
        mKV.encode(key, str);
    }

    @Override
    public String getString(String key) {
        return   mKV.decodeString(key);
    }

    @Override
    public String getString(String key, String def) {
        return mKV.decodeString(key, def);
    }

    @Override
    public void setLong(String key, long value) {
        mKV.encode(key, value);
    }

    @Override
    public long getLong(String key) {
        return  mKV.decodeLong(key);
    }

    @Override
    public long getLong(String key, long def) {
        return  mKV.decodeLong(key, def);
    }


    @Override
    public void setDouble(String key, double value) {
        mKV.encode(key, value);
    }

    @Override
    public double getDouble(String key) {
        return  mKV.decodeDouble(key);
    }

    @Override
    public double getDouble(String key, double def) {
        return  mKV.decodeDouble(key, def);
    }



    @Override
    public void removeValueForKey(String key) {
        mKV.removeValueForKey(key);
    }






    public void setMainBean(MainBean bean){

//         int fydjLev =3;//负压等级
//         int zrmsType =1;//注入模式
//         int tjsdType =1;//推进速度
//
//         int zjjlNum =3;//针剂剂量
//         int djjlNum =3;//单针剂量
//         int zscsNumMax =100;//注射次数最大
//         int modeType=1;//模式几

        setInt("Main_"+bean.modeType+"fydjLev",bean.fydjLev);
        setInt("Main_"+bean.modeType+"zrmsType",bean.zrmsType);
        setInt("Main_"+bean.modeType+"tjsdType",bean.tjsdType);

        setInt("Main_"+bean.modeType+"zjjlNum",bean.zjjlNum);
//        setInt("Main_"+bean.modeType+"djjlNum",bean.djjlNum);
        setInt("Main_"+bean.modeType+"zscsNumMax",bean.zscsNumMax);


    }


    public MainBean getMainBean(int modeType){

//        int fydjLev =3;//负压等级
//        int zrmsType =1;//注入模式
//        int tjsdType =1;//推进速度
//
//        int zjjlNum =3;//针剂剂量
//        int djjlNum =3;//单针剂量
//        int zscsNumMax =100;//注射次数最大
//         int modeType=1;//模式几

        MainBean bean = new MainBean();
        bean.modeType=modeType;

        bean.fydjLev=getInt("Main_"+bean.modeType+"fydjLev",bean.fydjLev);
        bean.zrmsType=  getInt("Main_"+bean.modeType+"zrmsType",bean.zrmsType);
        bean.tjsdType= getInt("Main_"+bean.modeType+"tjsdType",bean.tjsdType);

        bean.zjjlNum= getInt("Main_"+bean.modeType+"zjjlNum",bean.zjjlNum);
//        bean.djjlNum=  getInt("Main_"+bean.modeType+"djjlNum");
        bean.zscsNumMax= getInt("Main_"+bean.modeType+"zscsNumMax",bean.zscsNumMax);

        return bean;
    }


    public void setDebugItemBean(DebugItemBean bean){
        setDouble("D"+"_"+/*bean.getModeType()+"_"+*/bean.getType()+"_"+bean.getPosition(),bean.getNum());
    }

    public DebugItemBean getDebugItemBean(DebugItemBean bean){
        double num = getDouble("D"+"_"+/*bean.getModeType()+"_" +*/ bean.getType() + "_" + bean.getPosition(),bean.getNum());
        bean.setNum(num);
        return bean;
    }


    public void setDebugBean(DebugBean bean){
        //修改针的计量时,刷新针的默认选择
//       int beforePush= getInt("D_push"+bean.modeType,bean.push);
//        setDouble("D_fast"+bean.modeType,bean.speed_fast);
//        setDouble("D_nor"+bean.modeType,bean.speed_nor);
//        setDouble("D_slow"+bean.modeType,bean.speed_slow);
//        setInt("D_push"+bean.modeType,bean.push);
//        if (bean.push!= beforePush) {
//            setInt("Main_"+bean.modeType+"zjjlNum",0);
//        }

        setDouble("D_fast",bean.speed_fast);
        setDouble("D_nor",bean.speed_nor);
        setDouble("D_slow",bean.speed_slow);
        setInt("D_push",bean.push);
    }

    public int getPush(){
        DebugBean bean = new DebugBean();
       return getInt("D_push",bean.push);
    }

    public DebugBean getDebugBean(DebugBean bean){
//        bean.speed_fast=getDouble("D_fast"+bean.modeType,bean.speed_fast);
//        bean.speed_nor= getDouble("D_nor"+bean.modeType,bean.speed_nor);
//        bean.speed_slow= getDouble("D_slow"+bean.modeType,bean.speed_slow);
//        bean.push= getInt("D_push"+bean.modeType,bean.push);

        bean.speed_fast=getDouble("D_fast",bean.speed_fast);
        bean.speed_nor= getDouble("D_nor",bean.speed_nor);
        bean.speed_slow= getDouble("D_slow",bean.speed_slow);
        bean.push= getInt("D_push",bean.push);
        return bean;
    }

}
